/*
 * HQLStatement.java
 * 
 * Created on 3.10.2007, 21:52:31
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.easyedu.dictionary.api.util;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author hlavki
 */
public class EQLStatement {

    private String text;
    private List<Object> params;

    public EQLStatement() {
        this("", new ArrayList<Object>());
    }

    public EQLStatement(String text, List<Object> params) {
        this.text = text;
        this.params = params;
    }

    public List getParams() {
        return params;
    }

    public String getHQL() {
        return text;
    }

    public EQLStatement append(String text) {
        this.text += text;
        return this;
    }

    public EQLStatement prepend(String text) {
        this.text = text + this.text;
        return this;
    }

    public void prepareParams(PreparedStatement stmt) throws SQLException {
        for (int i = 1; i <= getParams().size(); i++) {
            stmt.setObject(i, getParams().get(i - 1));
        }
    }

    public void addParameter(Object param) {
        params.add(param);
    }

    public void merge(EQLStatement stmt) {
        if (stmt == null) {
            return;
        }
        append(" AND " + stmt.getHQL());
        for (int i = 0; i < stmt.getParams().size(); i++) {
            params.add(stmt.getParams().get(i));
        }
    }

    @Override
    public String toString() {
        return text + "[" + params + "]";
    }
}
